/* ----------------------------------------------------------------------------
* File: quiz_module_impl.h
*
* Desc: Quiz module implementation
*
* Created: 05/05/2011, by Alexey Korshunov
* ---------------------------------------------------------------------------- */
#ifndef _QUIZ_MODULE_IMPL_H_
#define _QUIZ_MODULE_IMPL_H_

#include <atlbase.h>
#include <atlcom.h>

#include "api_command_dispatcher.h"
#include "api_event_dispatcher.h"
#include "api_message_sender.h"
#include "debug_logger.h"
#include "exception.h"
#include "game_controller.h"
#include "thread_wrapper.h"
#include "quiz_module.h"
#include "quiz_common.h"
#include "quiz_statistic.h"
#include "utils.h"

namespace QuizCore
{
	class APIEventDispatcherHolder;
	class APIMessageSenderHolder;
	class StatProviderHolder;
	class GameControllerHolder;

	class QuizModuleImpl 
		: public QuizModule
		, public GameEventHandler
	{
		struct QuizInternalSettings
		{
			std::auto_ptr<QuizCore::DictionaryManager> Dict;
			std::auto_ptr<QuizCore::UserProfileManager> UserProfile;
			std::auto_ptr<SkypeProtection::APIAttachEventHandler> AttachHandler;
			std::auto_ptr<Common::Utils::ConcurrentQueue<MsgInfo>> InputMsgQueue;
			std::string ChatName;
			bool AutoPrompts;
			unsigned int PromptTime;
			unsigned int DelayQuestion;
			unsigned int QuestionTimeout;

			QuizInternalSettings()
				: AutoPrompts(false)
				, PromptTime(0)
				, DelayQuestion(0)
				, QuestionTimeout(0)
			{
			}
		};

	public:
		QuizModuleImpl();
		~QuizModuleImpl();

		// BaseModule implementation
		virtual Common::Error RunLogic();
		virtual Common::Error BeforeRun();
		virtual Common::Error AfterRun();

		// QuizModule implementation
		virtual Common::Error ShowCommonStatistics();
		virtual Common::Error ShowUserStatistics();
		virtual Common::Error ShowPrompt();
		virtual Common::Error NextQuestion();

		// GameEventHandler implementation
		void QuestionDelayNotify(const QuestionDelayNotifyParams& params);
		void QuestionNotify(const QuestionNotifyParams& params);
		void CorrectAnswerNotify(const CorrectAnswerNotifyParams& params);
		void TimeoutNotify(const TimeoutNotifyParams& params);
		void PromptNotify(const PromptNotifyParams& params);
		void NextQuestionNotify(const NextQuestionNotifyParams& params);

		void Init(const QuizModuleParams& params) throw(Common::Exception);

	private:
		std::auto_ptr<GameControllerHolder> CreateGameControllerHolder()
			throw(Common::Exception);
		std::auto_ptr<APIEventDispatcherHolder> CreateAPIEventDispatcherHolder()
			throw(Common::Exception);
		std::auto_ptr<APIMessageSenderHolder> CreateAPIMessageSenderHolder()
			throw(Common::Exception);
		std::auto_ptr<StatProviderHolder> CreateStatProviderHolder()
			throw(Common::Exception);

		void StartGame();
		void StopGame();

		QuizInternalSettings m_Settings;

		std::auto_ptr<Common::Logger> m_Logger;

		SkypeProtection::APIEventDispatcher* m_SkypeEventDispatcher;
		SkypeProtection::APIMessageSender* m_SkypeMessageSender;
		StatisticProvider* m_StatProvider;
		GameController* m_GameController;

		HANDLE m_EventPushQueue;
		HANDLE m_EventPopQueue;
	};
}

#endif // _QUIZ_MODULE_IMPL_H_
